package weaver.interfaces.jjy.duss.hr;

import com.sap.mw.jco.JCO;
import weaver.general.BaseBean;
import weaver.interfaces.jjy.duss.util.SAPComMehtod;
import weaver.interfaces.jjy.duss.util.SAPConnPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: ecology-9-demo
 * @description: 新员工入职&返聘
 * @author: Dusongsong
 * @create: 2019-11-26
 */
public class HR093 {
    public Map<String, String> execute(Map<String, String> map, List<Map<String, String>> mapList1, List<Map<String, String>> mapList2, List<Map<String, String>> mapList3) {
        BaseBean bb = new BaseBean();
        bb.writeLog("in————HR093");

        SAPConnPool SAPConn = new SAPConnPool();
        JCO.Client myConnection = SAPConn.getConnection();
        JCO.Function bapi = SAPConn.excuteBapi("ZFM_HR_PERNR");

        JCO.ParameterList input = bapi.getImportParameterList();//输入参数和结构处理
        JCO.ParameterList inputtable = bapi.getTableParameterList();//输入表的处理

        //入参为字段
        input.setValue("HR009", "JIEKNO");                   //HR接口标识：HR093入职&返聘接口
        input.setValue(map.get("PS_PHOTO"), "PS_PHOTO");
        input.setValue(map.get("IV_PERNR"), "IV_PERNR");
        //入参为结构
        JCO.Structure stu0 = input.getStructure("PS0000");
        stu0.setValue(map.get("BEGDA"), "BEGDA");            //入职日期
        stu0.setValue(map.get("MASSN"), "MASSN");            //事件类型
        stu0.setValue(map.get("MASSG"), "MASSG");            //事件原因

        JCO.Structure stu1 = input.getStructure("PS0001");
        stu1.setValue(map.get("PLANS"), "PLANS");            //岗位
        stu1.setValue(map.get("BEGDA4"), "BEGDA");            //入职日期
        stu1.setValue(map.get("ZHR_GWXL"), "ZHR_GWXL");      //职务序列
        stu1.setValue(map.get("ZHR_ZW"), "ZHR_ZW");          //职务名称
        stu1.setValue(map.get("ZHR_ZWC"), "ZHR_ZWC");        //职务簇
        stu1.setValue(map.get("WERKS"), "WERKS");            //人事范围
        stu1.setValue(map.get("BTRTL"), "BTRTL");            //人事子范围
        stu1.setValue(map.get("PERSG"), "PERSG");            //员工组
        stu1.setValue(map.get("PERSK"), "PERSK");            //员工子组
        stu1.setValue(map.get("ABKRS"), "ABKRS");            //工资核算范围

        JCO.Structure stu2 = input.getStructure("PS0002");
        stu2.setValue(map.get("NACHN"), "NACHN");            //姓名
        stu2.setValue(map.get("GESCH"), "GESCH");            //性别
        stu2.setValue(map.get("ZHR_JG"), "ZHR_JG");          //籍贯
        stu2.setValue(map.get("GBDAT"), "GBDAT");            //出生日期
        stu2.setValue(map.get("FAMST"), "FAMST");            //婚姻状况
        stu2.setValue(map.get("NATIO"), "NATIO");            //国籍
        stu2.setValue(map.get("ZHR_JKZK"), "ZHR_JKZK");      //健康状况
        stu2.setValue(map.get("ZHR_MZ"), "ZHR_MZ");          //民族


        JCO.Structure stu9 = input.getStructure("PS0009");
        stu9.setValue(map.get("BANKL"), "BANKL");            //银行代码
        stu9.setValue(map.get("BANKN"), "YHKH");            //银行卡号
        stu9.setValue(map.get("ZLSCH"), "ZLSCH");            //发放方式

        JCO.Structure stu3527 = input.getStructure("PS3527");
        stu3527.setValue(map.get("HUKOT"), "HUKOT");           //户口类型：2-农业户口

        JCO.Structure stu13 = input.getStructure("PS9007");
        stu13.setValue(map.get("ZHR_YGDJ"), "ZHR_YGDJ");     //员工等级

        JCO.Structure stu15 = input.getStructure("PS9012");
        stu15.setValue(map.get("ZHR_ZZCXY"), "ZHR_ZZCXY");   //专职促销员
        stu15.setValue(map.get("ZHR_SSGC"), "ZHR_SSGC");     //所属工厂
        stu15.setValue(map.get("ZHR_SSCJ"), "ZHR_SSCJ");     //所属车间

        //入参为表
        JCO.Table t6 = inputtable.getTable("PT0006");
        t6.appendRow();
        t6.setValue(map.get("ANSSA"), "ANSSA");            //地址类型
        t6.setValue(map.get("PSTLZ"), "PSTLZ");            //邮政编码
        t6.setValue(map.get("LOCAT"), "LOCAT");            //身份证地址
        t6.appendRow();
        t6.setValue(map.get("ANSSA1"), "ANSSA");           //地址类型
        t6.setValue(map.get("LOCAT1"), "LOCAT");           //现住址
        t6.setValue(map.get("PSTLZ1"), "PSTLZ");           //现住址编码

        JCO.Table t16 = inputtable.getTable("PT0016");
        t16.appendRow();
        t16.setValue(map.get("CTTYP"), "CTTYP");            //合同类型
        t16.setValue(map.get("CTBEG"), "CTBEG");            //合同开始日期
        t16.setValue(map.get("CTEDT"), "CTEDT");            //合同结束日期

        JCO.Table t19 = inputtable.getTable("PT0019");
        t19.appendRow();
        t19.setValue(map.get("TERMN"), "TERMN");            //任务日期（总部）
        t19.setValue(map.get("TMART"), "TMART");            //任务类型:Z3-转正

        JCO.Table t21 = inputtable.getTable("PT0021");
        for (Map<String, String> map1 : mapList1) {
            t21.appendRow();
            t21.setValue(map1.get("SUBTY"), "SUBTY");            //关系类型
            t21.setValue(map1.get("FANAM"), "FANAM");            //姓名
            t21.setValue(map1.get("FGBDT"), "FGBDT");            //出生日期
            t21.setValue(map1.get("ZHR_GZDW"), "ZHR_GZDW");      //工作单位
            t21.setValue(map1.get("ZHR_GW"), "ZHR_GW");          //职位/职务
            t21.setValue(map1.get("ZHR_TELNR"), "ZHR_TELNR");    //联系电话
            t21.setValue(map1.get("ZHR_DZ"), "ZHR_DZ");          //详细地址
            t21.setValue(map1.get("ZHR_SAMER"), "ZHR_SAMER");    //是否工作同一个公司
            bb.writeLog("HR093--map1=" + map1.toString());
        }
        JCO.Table t22 = inputtable.getTable("PT0022");
        t22.appendRow();
        t22.setValue(map.get("BEGDA1"), "BEGDA");            //学历开始日期
        t22.setValue(map.get("ENDDA1"), "ENDDA");            //学历结束日期
        t22.setValue(map.get("INSTI"), "INSTI");             //学校名称
        t22.setValue(map.get("ANZKL"), "ANZKL");             //学制
        t22.setValue(map.get("SLABS"), "SLABS");             //学位

        JCO.Table t23 = inputtable.getTable("PT0023");
        JCO.Table t3528 = inputtable.getTable("PT3528");//与表00023依存，一一对应
        for (Map<String, String> map2 : mapList2) {
            t23.appendRow();
            t23.setValue(map2.get("BEGDA2"), "BEGDA");           //履历开始日期
            t23.setValue(map2.get("ENDDA2"), "ENDDA");           //履历结束日期
            t23.setValue(map2.get("ARBGB"), "ARBGB");            //工作单位
            t23.setValue(map2.get("ZHR_JOBINFO"), "ZHR_JOBINFO");//履历职务
            t23.setValue(map2.get("ZHR_PLANS"), "ZHR_PLANS");   //履历岗位
            t23.setValue(map2.get("ZHR_JLLX"), "ZHR_JLLX");     //经历类型，10内部，20外部
            t3528.appendRow();
            t3528.setValue(map2.get("DEPTN"), "DEPTN");          //工作部门
            t3528.setValue(map2.get("REFER"), "REFER");          //证明人
            t3528.setValue(map2.get("REFCO"), "REFCO");          //证明人联系电话
            bb.writeLog("HR093--map2=" + map2.toString());
        }
        JCO.Table t105 = inputtable.getTable("PT0105");
        t105.appendRow();
        t105.setValue(map.get("USRTY"), "USRTY");           //通讯类型
        t105.setValue(map.get("USRID"), "USRID");           //手机号
        t105.appendRow();
        t105.setValue(map.get("USRTY1"), "USRTY");           //通讯类型
        t105.setValue(map.get("USRID1"), "USRID");           //福利卡号

        JCO.Table t185 = inputtable.getTable("PT0185");
        t185.appendRow();
        t185.setValue(map.get("ICNUM"), "ICNUM");           //证件编号
        t185.setValue(map.get("ICTYP"), "ICTYP");           //证件类别

        JCO.Table t3211 = inputtable.getTable("PT3211");
        t3211.appendRow();
        t3211.setValue(map.get("ZHR_QXM"), "ZHR_QXM");      //合同期限（月）
        t3211.setValue(map.get("ZHR_WRKPL"), "ZHR_WRKPL");  //合同签订单位
        t3211.setValue(map.get("ZHR_HTMB"), "ZHR_HTMB");    //劳动合同模板
        t3211.setValue(map.get("ZHR_BDLX"), "ZHR_BDLX");    //合同变动类型

        JCO.Table t3529 = inputtable.getTable("PT3529");//与表0022依存，一一对应
        t3529.appendRow();
        t3529.setValue(map.get("ACAQU"), "ACAQU");         //学历
        t3529.setValue(map.get("FACH3"), "FACH3");         //专业
        t3529.setValue(map.get("INSMO"), "INSMO");         //学习形式
        t3529.setValue(map.get("ACQID"), "ACQID");         //学历证书编号
        t3529.setValue(map.get("ACCID"), "ACCID");         //学位证书编号

        JCO.Table t3531 = inputtable.getTable("PT3531");
        for (Map<String, String> map3 : mapList3) {
            t3531.appendRow();
            t3531.setValue(map3.get("BEGDA3"), "BEGDA");        //资格开始日期
            t3531.setValue(map3.get("ENDDA3"), "ENDDA");        //资格结束日期
            t3531.setValue(map3.get("ISEBY"), "ISEBY");         //批准（鉴定）机构
            t3531.setValue(map3.get("QUCEC"), "QUCEC");         //资格名称
            t3531.setValue(map3.get("QUCEE"), "QUCEE");         //资格等级
            t3531.setValue(map3.get("QUCEG"), "QUCEG");         //资格类型
            t3531.setValue(map3.get("QUCEN"), "QUCEN");         //证书编号
            bb.writeLog("HR093--map3=" + map3.toString());

        }
        //执行函数
        myConnection.execute(bapi);
        JCO.ParameterList out = bapi.getExportParameterList();//输出参数和结构处理
        JCO.ParameterList Table00 = bapi.getTableParameterList();//输出表的处理

        //返回处理结果以及日志
        Map<String, String> mapreturn = new HashMap<>();
        BaseBean baseBean = new BaseBean();
        JCO.Structure stu = out.getStructure("EV_OUTPUT");
        mapreturn.put("flag",(String) stu.getValue("ZFLAG"));
        mapreturn.put("message",(String) stu.getValue("ZMESG"));
        if("E".equals(mapreturn.get("flag"))){
            //遍历
            String structure = baseBean.getPropValue("SAPAllTable","structure");//所有结构
            String[] structures = structure.split(",");
            String table = baseBean.getPropValue("SAPAllTable","table");//所有表
            String[] tables = table.split(",");
            mapreturn = SAPComMehtod.SAPreturnS(structures, out, tables, Table00);
        }
        String pernr = (String)out.getValue("EV_PERNR");
        mapreturn.put("pernr",pernr);
        baseBean.writeLog("SAP返回结果"+mapreturn.toString());
        JCO.releaseClient(myConnection);
        return mapreturn;
    }

}
